package com.zcc.makingfriend.controller;

import com.zcc.makingfriend.dao.Comment;
import com.zcc.makingfriend.dto.CommentDto;
import com.zcc.makingfriend.service.CommentService;
import com.zcc.makingfriend.untils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zcc
 */
@Api(tags = "评论管理")
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    CommentService commentService;

    @ApiOperation("新增评论")
    @PostMapping("/add")
    public Result addComment(@RequestBody Comment comment) {
        int i = commentService.addComment(comment);
        if (i == 1) {
            return Result.success("新增评论成功");
        }
        return Result.fail("新增评论失败");
    }

    @ApiOperation("查询评论")
    @GetMapping("/find/{articleId}")
    public Result findComment(@PathVariable Long articleId) {
        List<CommentDto> commentsByArticleId = commentService.findCommentsByArticleId(articleId);
        return Result.success(commentsByArticleId);
    }

    @ApiOperation("举报评论")
    @GetMapping("/report/{id}")
    public Result reportComment(@PathVariable Long id) {
        int i = commentService.reportComment(id);
        if (i == 1) {
            return Result.success("举报评论成功");
        }
        return Result.fail("举报评论失败");
    }

    @ApiOperation("删除评论")
    @DeleteMapping("/delete/{id}")
    public Result deleteComment(@PathVariable Long id) {
        int i = commentService.deleteComment(id);
        if (i == 1) {
            return Result.success("删除评论成功");
        }
        return Result.fail("删除评论失败");
    }

    @ApiOperation("是否点赞评论")
    @GetMapping("/isLike/{id}/{isLike}/{likes}")
    public Result isLikeComment(@PathVariable Long id, @PathVariable int isLike, @PathVariable Long likes) {
        int likeComment = commentService.isLikeComment(id, isLike, likes);
        if (likeComment == 1) {
            return Result.success(isLike == 1? "点赞" : "取消点赞" + "成功");
        }
        return Result.success(isLike == 1? "点赞" : "取消点赞" + "失败");
    }
}
